/**
 @Name：dist 数据字典
 @Author：xiaolai
 @Site：http://www.9dyun.cn
 @License：9DIAN

 */
layui.define(function (exports) {
    //公共事件
    var event = {
        get: function(distName) {
           return  sessionStorage.getItem(distName)||'';
        },
        set: function(datas) {
           $.each(datas,function () {
               console.log(this.key,this.value)
               sessionStorage.setItem(this.key,JSON.stringify(this.value));
           })
        },
        ajax:function(){
            ApiUtil.get('/common/enums', {}, function (resp) {
                event.set(resp.data);
            })
        },
        getDict: function (distName) {
            return JSON.parse(event.get(distName)||'{}')
        }
    }
    /**
     * form = new Form('formId');
     * @constructor
     */
    var dict = function () {}
    dict.prototype = {
        init:function(){
            event.ajax();
        },
        get: function (distName) {
            return  event.getDict(distName);
        },
        getValue: function (distName,key) {
            var enumObj=event.getDict(distName);
            var value='';
            if(enumObj){
                $.each(enumObj,function () {
                    if(this.key==key){
                        value=this.value;
                        return false;
                    }
                })
            }
            return value;
        },
        getKey: function (distName,value) {
            var enumObj=event.getDict(distName);
            var key='';
            if(enumObj){
                $.each(enumObj,function () {
                    if(this.value==value){
                        key=this.key;
                        return false;
                    }
                })
            }
            return key;
        }

    };
    exports('dict', function () {
        return new dict();
    });
});
